﻿Option Strict On
Option Explicit On 

Imports System.Windows.Forms
Imports IP.Core.IPUserService

''' -----------------------------------------------------------------------------
''' Project	 : IPDataControl
''' Class	 : Core.IPDataControl.CDataAndFormControl
''' 
''' -----------------------------------------------------------------------------
''' <summary>
''' Cung cấp tiện ích xử lý form DE bao gồm DATASOURCE và FORM control
''' </summary>
''' <example>
''' Phải thực hiện theo các bước như sau
''' <list type="bullet">
''' <item> A) implement 1 interface để có thể điều khiển form: I_DEDataContainer</item>
''' <item> B) khởi tạo đối tượng control (thường làm ở các bước set_form_intial</item>
''' <item> C) start form trong 1 mode nào đó </item>
''' </list>
'''
''' Bước A) implement interface I_DEDataContainer
''' <code language="VB.NET">
''' 
''' #Region "IMPLEMENT DEDataContainer"
'''
'''
''' Sub datarow_2_form(ByVal i_datarow As DataRow) Implements I_DEDataContainer.datarow_2_form
'''    Dim v_us As New US_HT_NGUOI_SU_DUNG
'''    v_us.DataRow2Me(i_datarow)
'''    Me.us_object_2_form(v_us)
''' End Sub
'''
''' Sub clear_data_in_form() Implements I_DEDataContainer.clear_data_in_form
'''
'''    m_txt_password.Text = ""
'''    m_txt_password_verify.Text = ""
'''    Me.m_txt_username.Text = ""
'''    Me.m_txt_ten_that_cua_user.Text = ""
'''    Me.m_cbo_thuoc_nhom.SelectedIndex = 0
'''    Me.m_cbo_user_status.SelectedValue = US_HT_NGUOI_SU_DUNG.get_default_user_status
''' End Sub
'''
''' Sub submit_insert_to_db(ByRef o_us_object As US_Object, ByRef o_succeeded As Boolean) Implements I_DEDataContainer.submit_insert_to_db
'''    Dim v_us_user As New US_HT_NGUOI_SU_DUNG
'''    Me.form_2_us_object(v_us_user)
'''    v_us_user.strCREATED_BY = CApplicationContext.get_curent_user
'''    v_us_user.strUPDATED_BY = CApplicationContext.get_curent_user
'''    v_us_user.datCREATED_DATE = CApplicationContext.get_current_business_date
'''    v_us_user.datUPDATED_DATE = CApplicationContext.get_current_business_date
'''    Try
'''        v_us_user.open_connection()
'''        v_us_user.Insert()
'''        v_us_user.commit_close_connection()
'''        o_us_object = v_us_user
'''        o_succeeded = True
'''    Catch v_ex As Exception
'''        If v_us_user.is_having_connection Then
'''            v_us_user.rollback_close_connection()
'''        End If
'''        CExpUtils.DefaultDBExpHandler(v_ex)
'''        o_succeeded = False
'''    End Try
''' End Sub
'''
''' Sub submit_update_to_db(ByVal i_current_datarow As DataRow, ByRef o_us_object As US_Object, ByRef o_succeeded As Boolean) _
'''                    Implements I_DEDataContainer.submit_update_to_db
'''    Dim v_us_user As New US_HT_NGUOI_SU_DUNG
'''    v_us_user.DataRow2Me(i_current_datarow)
'''    Me.form_2_us_object(v_us_user)
'''    v_us_user.strUPDATED_BY = CApplicationContext.get_curent_user
'''    v_us_user.datUPDATED_DATE = CApplicationContext.get_current_business_date
'''    Try
'''        v_us_user.open_connection()
'''        v_us_user.Update()
'''        v_us_user.commit_close_connection()
'''        o_us_object = v_us_user
'''        o_succeeded = True
'''    Catch v_ex As Exception
'''        If v_us_user.is_having_connection Then
'''            v_us_user.rollback_close_connection()
'''        End If
'''        CExpUtils.DefaultDBExpHandler(v_ex)
'''        o_succeeded = False
'''    End Try
'''  End Sub
'''
'''  Sub submit_delete_current(ByVal i_current_datarow As DataRow, ByRef o_succeeded As Boolean) _
'''                    Implements I_DEDataContainer.submit_delete_current
'''    Dim v_us_user As New US_HT_NGUOI_SU_DUNG
'''    v_us_user.DataRow2Me(i_current_datarow)
'''    Try
'''        v_us_user.open_connection()
'''        v_us_user.DeleteByID(v_us_user.dcID)
'''        v_us_user.commit_close_connection()
'''        o_succeeded = True
'''    Catch ex As Exception
'''        If v_us_user.is_having_connection Then
'''            v_us_user.rollback_close_connection()
'''        End If
'''        o_succeeded = False
'''    End Try
'''  End Sub
'''
'''  Sub start_insert_new(ByRef o_is_insertable As Boolean) Implements I_DEDataContainer.start_insert_new
'''    ' ở đây có thể check xem có insert được không, bạn có thể cấm insert nếu bạnk hông muốn cho user insert mới
'''    o_is_insertable = True
'''  End Sub
'''
'''  Sub start_update(ByVal i_current_datarow As DataRow, ByRef o_is_updatable As Boolean) _
'''                    Implements I_DEDataContainer.start_update
'''     ' đây là chỗ để check xem có update được không, bạn có thể  lock record ở đây
'''     o_is_updatable = True
'''  End Sub
'''
'''  Sub start_delete(ByVal i_current_datarow As DataRow, ByRef o_is_deletable As Boolean) Implements I_DEDataContainer.start_delete
'''   ' đây là chỗ để check xem có delete được không
'''    o_is_deletable = True
'''  End Sub
'''
'''#End Region
'''
''' </code> 
''' 
''' 
''' Bước B) Khởi tạo control điều khiển CDataAndFormControl
''' <code language="VB.NET">
'''        ' bước 01. tại đây bạn khởi tạo 1 control cho form và các .net control
'''        Dim v_form_control As New CDEFormStateControl(m_stb_fms_style)
'''        v_form_control.add_de_button(IPDEButtonEnum.Save, m_cmd_save)
'''        v_form_control.add_de_button(IPDEButtonEnum.MovePrevious, m_cmd_previous)
'''        v_form_control.add_de_button(IPDEButtonEnum.MoveNext, m_cmd_next)
'''        v_form_control.add_de_button(IPDEButtonEnum.Update, m_cmd_update)
'''        v_form_control.add_de_button(IPDEButtonEnum.Delete, m_cmd_xoa)
'''        v_form_control.add_de_button(IPDEButtonEnum.Insert, m_cmd_insert)
'''        v_form_control.add_de_button(IPDEButtonEnum.Cancel, m_cmd_cancel)
'''        v_form_control.add_de_button(IPDEButtonEnum.Search, m_cmd_search)
'''        v_form_control.addTextBox(m_txt_password)
'''        v_form_control.addTextBox(m_txt_password_verify)
'''        v_form_control.addTextBox(m_txt_ten_that_cua_user)
'''        v_form_control.addTextBox(m_txt_username)
'''        v_form_control.addComboBox(m_cbo_thuoc_nhom)
'''        v_form_control.addComboBox(m_cbo_user_status)
'''        ' bước 02. tại đây bạn khởi tạo 1 datasource control.
'''        ' chú ý là dataset để nhét vào datasource của bạn có thể là rỗng (dataset mới tinh)
'''        Dim v_datasource_ctl As New CFormDEDataSourceControl(m_ds_user.HT_NGUOI_SU_DUNG)
'''        ' bước 03. tại đây bạn làm 1 đối tượng để control cả 2 thứ: control và data
'''        m_ctl_form_and_datasource = New CDataAndFormControl(v_datasource_ctl, v_form_control, Me)
''' 
''' </code>
''' 
''' Bước C) Khởi động form DE trong 1 mode thích hợp
''' Sau đó bạn đã có thể sử dụng cái này. Trong ví dụ sau tôi start nó theo 2 kiểu khác nhau
''' <code language = "VB.NET">
'''  Private Sub set_form_initial()
'''    set_controls_start_state()
'''    Select Case m_st_start_state
'''        Case DataEntryFormMode.InsertDataState
'''            m_ctl_form_and_datasource.start_in_insert_mode()  'tôi start bằng insert mode
'''        Case DataEntryFormMode.ViewDataState
'''            m_ctl_form_and_datasource.start_in_view_mode(m_us_start_user.dcID) ' tôi start bằng view mode
'''    End Select
'''End Sub
''' </code>
''' </example>
''' <remarks>
''' </remarks>
''' <history>
''' 	[csung]	3/17/2005	Created
''' </history>
''' -----------------------------------------------------------------------------
Public Class CDataAndFormControl



#Region "MEMBERS"
    Private m_ctl_datasource_control As CFormDEDataSourceControl
    Private m_ctn_de_container As I_DEDataContainer
    Private m_ctl_form_control As CDEFormStateControl
#End Region

#Region "PUBLIC"

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <param name="i_ctl_datasource_control">datasource control</param>
    ''' <param name="i_ctl_form_control">form control đã có các button và field cần xử lý</param>
    ''' <param name="i_ctn_DE_container">container để gọi ngược lại</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub New(ByVal i_ctl_datasource_control As CFormDEDataSourceControl, _
                   ByVal i_ctl_form_control As CDEFormStateControl, _
                   ByVal i_ctn_DE_container As I_DEDataContainer)
        m_ctl_form_control = i_ctl_form_control
        m_ctl_datasource_control = i_ctl_datasource_control
        m_ctn_de_container = i_ctn_DE_container
        set_event_handlers()
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Start in View mode, có specify đứng ở record nào
    ''' </summary>
    ''' <param name="i_dc_id">ID của hàng cần đứng</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub start_in_view_mode(ByVal i_dc_id As Decimal)
        Debug.Assert(m_ctl_datasource_control.get_number_of_rows > 0, "Không có dữ liệu thì không thể đứng ở record nào được - Csung")
        If m_ctl_datasource_control.get_number_of_rows > 0 Then   ' nếu có dữ liệu thì hiên thị
            set_index_at(i_dc_id)
            Me.display_current_row()
            go_to_view_mode()
        End If
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' nếu bạn gọi form ở mode xem chi tiết thì bạn gọi cái này
    ''' </summary>
    ''' <remarks>
    ''' Nếu không có dữ liệu thì hiện thị ở mode view  và không có dữ liệu gì cả ( không
    ''' tự động chuyển sang mode new)
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/16/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub start_in_view_mode()
        m_ctn_de_container.clear_data_in_form()
        If m_ctl_datasource_control.get_number_of_rows > 0 Then   ' nếu có dữ liệu thì hiên thị
            Me.display_current_row()
        End If
        go_to_view_mode()
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Start bằng insert mode, nếu bạn gọi form ở mode này thì bạn hãy gọi mode này
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/16/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub start_in_insert_mode()
        Dim v_insertable As Boolean
        m_ctn_de_container.start_insert_new(v_insertable)
        Debug.Assert(v_insertable, "Không cho insert à? - CSUNG")
        If Not v_insertable Then Exit Sub

        m_ctn_de_container.clear_data_in_form()
        m_ctl_form_control.start_insert_new()
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Nếu bạn bắt đầu bằng cách udpate record này.
    ''' </summary>
    ''' <param name="i_dc_Id">ID của hàng mà bạn định update</param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/16/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Sub start_in_update_mode(ByVal i_dc_Id As Decimal)
        Debug.Assert(m_ctl_datasource_control.get_number_of_rows > 0, "Không có dữ liệu thì không thể đứng ở record nào được - Csung")
        If m_ctl_datasource_control.get_number_of_rows > 0 Then
            Me.set_index_at(i_dc_Id)
            Dim v_updatable As Boolean
            ''check whether data is updatable
            m_ctn_de_container.start_update(m_ctl_datasource_control.get_curent_datarow, v_updatable)
            If Not v_updatable Then
                go_to_view_mode()
            Else
                m_ctl_form_control.start_update()
            End If
        End If
    End Sub

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Trả lại datarow hiện tại
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_current_data_row() As DataRow
        Return m_ctl_datasource_control.get_curent_datarow
    End Function

    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Trạng thái hiện nay của form là gì
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/17/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Function get_current_form_state() As IPFormDEState
        Return m_ctl_form_control.get_state_of_ipde_form()
    End Function


#End Region

#Region "Private"

    Private Sub go_to_view_mode()
        m_ctl_form_control.go_to_view_mode()
        m_ctl_form_control.display_info(m_ctl_datasource_control.get_str_current_per_total)
    End Sub

    Private Sub submit_insert(ByRef o_succeeded As Boolean)
        Dim v_is_succeeded As Boolean = False
        Dim v_us_object_inserted As US_Object
        m_ctn_de_container.submit_insert_to_db(v_us_object_inserted, v_is_succeeded)
        o_succeeded = v_is_succeeded

        If v_is_succeeded Then
            m_ctl_datasource_control.add_data_row(v_us_object_inserted)
            go_to_view_mode()
        End If
    End Sub

    Private Sub submit_update(ByRef o_succeeded As Boolean)
        Dim v_is_succeeded As Boolean = False
        Dim v_us_object_updated As US_Object
        m_ctn_de_container.submit_update_to_db(m_ctl_datasource_control.get_curent_datarow, _
                                                v_us_object_updated, v_is_succeeded)
        o_succeeded = v_is_succeeded
        If v_is_succeeded Then
            m_ctl_datasource_control.update_current_data_row(v_us_object_updated)
            go_to_view_mode()
        End If
    End Sub

    Private Sub delete_current()
        Dim v_is_succeeded As Boolean = False
        Dim v_us_object_updated As US_Object
        m_ctn_de_container.submit_delete_current(m_ctl_datasource_control.get_curent_datarow, v_is_succeeded)
        If v_is_succeeded Then
            m_ctl_datasource_control.delete_current_datarow()
            display_current_row()
            go_to_view_mode()
        End If
    End Sub

    Private Sub display_current_row()
        '' hiện thị hàng hiện tại ra
        m_ctn_de_container.clear_data_in_form()
        Dim v_datarow As DataRow = m_ctl_datasource_control.get_curent_datarow()
        If Not v_datarow Is Nothing Then
            m_ctn_de_container.datarow_2_form(v_datarow)
            m_ctl_form_control.display_info(m_ctl_datasource_control.get_str_current_per_total)
        End If
    End Sub

    Private Sub set_event_handlers()
        Try
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.Cancel, AddressOf m_cmd_cancel_Click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.MoveNext, AddressOf m_cmd_next_click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.MovePrevious, AddressOf m_cmd_previous_click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.Delete, AddressOf m_cmd_delete_Click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.Insert, AddressOf m_cmd_insert_Click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.Update, AddressOf m_cmd_update_Click)
            m_ctl_form_control.add_de_click_event_handler(IPDEButtonEnum.Save, AddressOf m_cmd_save_Click)

            m_ctl_form_control.set_form_event_handler(AddressOf frm_KeyDown, AddressOf Me.frm_Closing)

        Catch v_e As Exception
            Debug.Assert(False, "lỗi khi set các event handlers cho các button - có thể bạn quyên chưa add các button cho controller ")
        End Try
    End Sub

    Private Sub submit_to_db(ByRef o_succeeded As Boolean)
        Select Case m_ctl_form_control.get_state_of_ipde_form
            Case IPFormDEState.DE_InsertState
                submit_insert(o_succeeded)
            Case IPFormDEState.DE_UpdateState
                submit_update(o_succeeded)
        End Select
    End Sub

    Private Sub cancel_editing()
        Me.display_current_row()
        m_ctl_form_control.go_to_view_mode()
    End Sub
#End Region

#Region "EVENTS HANDLERS"
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Khi đóng cửa sổ hỏi user có cập nhật dữ liệu không
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/18/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub frm_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        Select Case get_current_form_state()
            Case IPFormDEState.DE_ViewState
                '
            Case IPFormDEState.DE_InsertState, IPFormDEState.DE_UpdateState
                Select Case IP.Core.IPCommon.BaseMessages.MsgBox_YES_NO_CANCEL(11)
                    Case DialogResult.Yes
                        Dim v_succeeded As Boolean = False
                        Me.submit_to_db(v_succeeded)
                        e.Cancel = Not v_succeeded
                    Case DialogResult.No
                        e.Cancel = False
                    Case DialogResult.Cancel
                        e.Cancel = True
                End Select
        End Select
    End Sub



    Private Sub frm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Select Case e.KeyCode
            Case Keys.Left
                If e.Alt And m_ctl_form_control.get_state_of_ipde_form = IPFormDEState.DE_ViewState Then
                    Me.m_ctl_datasource_control.previous_row()
                    display_current_row()
                End If
            Case Keys.Right
                If e.Alt And m_ctl_form_control.get_state_of_ipde_form = IPFormDEState.DE_ViewState Then
                    Me.m_ctl_datasource_control.next_row()
                    display_current_row()
                End If
            Case Keys.Escape
                Select Case get_current_form_state()
                    Case IPFormDEState.DE_ViewState
                        m_ctl_form_control.form_in_control.Close()
                    Case Else
                        cancel_editing()
                End Select
        End Select
    End Sub

    Private Sub m_cmd_next_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.m_ctl_datasource_control.next_row()
        display_current_row()
    End Sub

    Private Sub m_cmd_previous_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.m_ctl_datasource_control.previous_row()
        display_current_row()
    End Sub

    Private Sub m_cmd_cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        ' khi ấn cancel thì quay lại 
        cancel_editing()
    End Sub

    Private Sub m_cmd_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim v_updatable As Boolean = True
        ''check whether data is updatable
        m_ctn_de_container.start_update(m_ctl_datasource_control.get_curent_datarow, v_updatable)
        If Not v_updatable Then Exit Sub
        m_ctl_form_control.start_update()
    End Sub

    Private Sub m_cmd_insert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim v_insertable As Boolean = True
        ''check whether data is updatable
        m_ctn_de_container.start_insert_new(v_insertable)
        If Not v_insertable Then Exit Sub
        m_ctn_de_container.clear_data_in_form()
        m_ctl_form_control.start_insert_new()
    End Sub

    Private Sub m_cmd_delete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Me.delete_current()
    End Sub

    Private Sub m_cmd_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim v_succeeded As Boolean
        submit_to_db(v_succeeded)
    End Sub


    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Set cho con trỏ của datasourc  ở đây
    ''' </summary>
    ''' <param name="i_dc_ID"></param>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    ''' 	[csung]	3/16/2005	Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Private Sub set_index_at(ByVal i_dc_ID As Decimal)
        m_ctl_datasource_control.set_index_at(i_dc_ID)
        display_current_row()
    End Sub
#End Region



End Class
